package com.wc.alorithm_luogu._前缀和与差分.火烧赤壁;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/1/9 15:33
 * @description
 * https://www.luogu.com.cn/problem/P1496
 */
public class Main {
    /**
     * 思路：
     * 差分 + 离散化 + 二分
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 60010;
    static int[] a = new int[N], b = new int[N], c = new int[N], d = new int[N];
    static int n, m;

    public static void main(String[] args) {
        n = sc.nextInt();
        for (int i = 1, j = 1; i <= n; i++) {
            c[j++] = a[i] = sc.nextInt();
            c[j++] = b[i] = sc.nextInt() - 1;
            c[j++] = b[i] + 1;
        }
        // 离散化步骤
        // 1、排序
        Arrays.sort(c, 1, 3 * n + 1);
        // 2、去重
        m = 1;
        for(int i = 1; i <= 3 * n; i++) {
            if (c[m] != c[i]) c[++m] = c[i];
        }

        for (int i = 1; i <= n; i++) {
            int l = find(a[i]), r = find(b[i]);
            d[l]++;
            d[r + 1]--;
        }
        for (int i = 1; i <= m; i++) d[i] += d[i - 1];
        int res = 0;
        for (int i = 1; i <= m; i++) {
            if (d[i] > 0) {
                int r = i + 1;
                while (d[r] > 0) r++;
                r--;
                res += c[r] - c[i] + 1;
                i = r + 1;
            }
        }
        out.println(res);
        out.flush();
    }

    static int find(int x) {
        int l = 1, r = m;
        while (l < r) {
            int mid = l + r >> 1;
            if (c[mid] >= x) r = mid;
            else l = mid + 1;
        }
        return l;
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

